package com.zhuanzhuan;
import com.zhuanzhuan.entity.*;
import com.zhuanzhuan.service.AdminService;
import com.zhuanzhuan.service.UserService;
import com.zhuanzhuan.service.impl.AdminServiceImpl;
import com.zhuanzhuan.service.impl.UserServiceImpl;
import java.util.Scanner;

public class ZhuanzhuanClient {
    private static Admin admin;
    private static User user;

    public static void main(String[] args) {
        ZhuanzhuanClient.startup();
    }

    //启动登录界面
    public static void startup(){
        String userType = null;
        String name = null;
        String pwd = null;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("用户登录,请输账号类型:1.普通用户\t2.管理员");
            System.out.print("请输入账号类型:");
            userType = input.next();
            System.out.print("请输入账号:");
            name = input.next();
            System.out.print("请输入密码:");
            pwd = input.next();
        }while(!login(userType, name, pwd));

        if(admin != null){
            showAdminMenu();
        }else{
            showUserMenu();
        }
    }

    //普通用户和管理员用户要有不同的登录处理逻辑
    public static boolean login(String userType, String name, String pwd){
        boolean isLoginSucess = false;
        if(userType.equals("1")){
            UserService userService = new UserServiceImpl();
            user = userService.login(name, pwd);
            if(user != null){
                isLoginSucess = true;
            }
        }else if(userType.equals("2")){
            AdminService adminService = new AdminServiceImpl();
            admin = adminService.login(name, pwd);
            if(admin != null){
                isLoginSucess = true;
            }
        }
        if(isLoginSucess){
            System.out.println("登录成功！");
        }else{
            System.out.println("用户名或密码错误，登录失败！");
        }
        return isLoginSucess;
    }

    //显示管理员操作主界面
    public static void showAdminMenu(){
        AdminService adminService = new AdminServiceImpl();
        while(true) {
            Scanner input = new Scanner(System.in);
            System.out.println("1.查看交易记录\n" +
                    "2.生成交易报告\n" +
                    "3.查看好物列表\n" +
                    "4.下架不合格商品\n" +
                    "5.封禁违规账号\n" +
                    "0.退出系统");
            System.out.print("请输入要执行的操作:");
            String action = input.next();
            if(action.equals("1")){
                adminService.listTradeLog();
            }else if(action.equals("3")){
                adminService.listGoods(-1);
            }else if(action.equals("0")){
                break;
            }else if(action.equals("2") || action.equals("4") || action.equals("5")){
                System.out.println("功能开发中...");
            }
            else{
                System.out.println("输入的操作编号错误！");
            }
        }
        System.out.println("再见！");
    }
    //显示普通用户的操作主界面
    public static void showUserMenu(){
        Scanner input = new Scanner(System.in);
        System.out.println("-----------您的基本信息------------");
        System.out.println("用户名："+user.getName());
        System.out.println("账户余额："+user.getBalance());
        UserService userService = new UserServiceImpl();
        while(true) {
            System.out.println("1.查看好物列表\n" +
                    "2.上架新的好物\n" +
                    "3.查看我的在售好物\n" +
                    "4.账户操作\n" +
                    "0.退出系统");
            System.out.print("请输入要执行的操作:");
            String action = input.next();
            if(action.equals("1")){
                userService.listGoods(user,-1);
            }else if(action.equals("2")){
                userService.addNewGoods(user);;
            }else if(action.equals("3")){
                userService.listUserGoods(user);
            }else if(action.equals("4")){
                System.out.println("功能开发中...");
            }else if(action.equals("0")){
                break;
            }else{
                System.out.println("输入的操作编号错误！");
            }
        }
        System.out.println("再见！");
    }
}
